// 饿汉式单例
// c++11 内存模型保证了 static 的线程安全性

#include <thread>
#include <vector>

template <typename T>
class Singleton
{
public:
    static T& getInstance()
    {
        static T inst;
        return inst;
    }

    Singleton() = delete;
    Singleton(const Singleton&) = delete;
    Singleton& operator=(const Singleton&) = delete;
};

/// test
struct A {};

int main(int argc, char *argv[])
{
    std::vector<std::thread> threads;

    for (int i = 0; i < 100; i++)
    {
        threads.emplace_back([&]{
            A& inst = Singleton<A>::getInstance();
            printf("instance address: %p\n", &inst);
        });
    }

    for (auto &t : threads)
    {
        t.join();
    }

    return 0;
}
